/*
 * NetErrorHelper.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.net.exception;

import android.text.TextUtils;

import com.google.gson.JsonParseException;
import com.yidiandao.business.R;
import com.yidiandao.business.constant.ServerCode;
import com.yidiandao.business.data.net.BaseRequest;
import com.yidiandao.business.utils.BuglyUtils;
import com.yidiandao.business.utils.ExceptionUtils;
import com.yidiandao.business.utils.LogUtils;
import com.yidiandao.business.utils.NetworkUtils;
import com.yidiandao.business.utils.UIUtils;

import java.io.IOException;
import java.net.SocketTimeoutException;

import static com.yidiandao.business.utils.UIUtils.getString;

/**
 * 网络错误帮助类.
 */
public class NetErrorHelper {

    /**
     * 网络错误的封装
     */
    public static NetErrorInfo generateNetErrorInfo(final Exception exception) {

        int code;

        String message;

        boolean networkAvailable = NetworkUtils.isNetworkAvailable(UIUtils.getContext());

        // 网络错误封装
        if (!networkAvailable) {
            // 网络无连接 --->  检查网络
            code = ServerCode.NO_CONNECT;
            message = getString(R.string.net_no_connect);
        } else if (exception instanceof JsonParseException) {
            // Json转换错误  --> 上传至友盟 包含Request --> Json
            code = ServerCode.JSON_PARSE_ERROR;
            message = getString(R.string.parse_error);
        } else if (exception instanceof SocketTimeoutException) {
            // 连接超时
            code = ServerCode.TIME_OUT;
            message = getString(R.string.net_time_out);
        } else if (exception instanceof IOException) {
            if ("Canceled".equals(exception.getMessage())
                    || "Socket closed".equals(exception.getMessage())) {
                code = ServerCode.CANCELED;
                message = null;
            } else {
                code = ServerCode.OTHER_NET_ERROR;
                message = getString(R.string.net_error);
            }
        } else if (exception instanceof NullPointerException) {
            code = ServerCode.NULL;
            message = UIUtils.getString(R.string.null_error_hint);
        } else {
            // 其他错误
            code = ServerCode.OTHER_NET_ERROR;
            message = getString(R.string.net_error);
        }
        return new NetErrorInfo(message, code, exception);
    }

    /**
     * 报告网络错误, 在前台打印日志信息, 远程通过友盟上传错误日志.
     */
    public static void reportError(final Class clazz, final String url, final BaseRequest request,
                                   final NetErrorInfo netErrorInfo, String responseBody) {

        if (clazz == null || TextUtils.isEmpty(url) || netErrorInfo == null) {
            return;
        }

        String content = null;
        int code = netErrorInfo.getCode();
        Exception exception = netErrorInfo.getException();

        switch (code) {
            case ServerCode.NO_CONNECT:
                // 网络无连接
                content = netErrorInfo.getMessage();
                break;
            case ServerCode.JSON_PARSE_ERROR:
                // Json转换错误 --> 上传至友盟
                content = "服务端响应结果解析异常";
                ExceptionUtils.addMessage(exception, clazz, "Response-Result : " + responseBody);
                ExceptionUtils.addMessage(exception, clazz, "Request-Params : " + (request == null ? "null" : request.toJson()));
                ExceptionUtils.addMessage(exception, clazz, "Server-ParserError, url : " + url);
                BuglyUtils.reportError(exception);
                break;
            case ServerCode.NULL:
                content = "Response 为空!!!";
                ExceptionUtils.addMessage(exception, clazz, "Response-Result : " + content);
                ExceptionUtils.addMessage(exception, clazz, "Request-Params : " + (request == null ? "null" : request.toJson()));
                ExceptionUtils.addMessage(exception, clazz, "Server-ParserError, url : " + url);
                BuglyUtils.reportError(exception);
                break;
            default:
                // 其他错误
                content = "其他异常, url : " + url;
                break;
        }

        // 前台打印错误信息
        LogUtils.e(content, exception);
    }
}
